package week04.file;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangxiaokun on 2018/3/1.
 */
public class FileDir extends File {
    private List<File> childFileList = new ArrayList<>();

    public FileDir(String path, String fileName) {
        super(path, fileName);
    }

    @Override
    public void open() {
        System.out.println("====打开目录" + getPath()+ "======");
        for (File file : childFileList) {
            System.out.println(file.getPath()   + "\t\t" + getSize());
        }
        System.out.println("=============================");
    }

    //添加单个文件
    @Override
    public void add(File file) {
        childFileList.add(file);
        setSize(getSize() + file.getSize());
    }

    @Override
    public boolean remove(File file) {
        if (childFileList.contains(file)) {
            childFileList.remove(file);
            setSize(getSize() - file.getSize());
            return true;
        }
        return false;
    }

    @Override
    public File getChild(int index) {
        if (index >= 0 && index < childFileList.size()) {
            return childFileList.get(index);
        }
        return null;
    }

    //添加多个文件
    @Override
    public void addFiles(File...fileList) {
        for (File file : fileList) {
            add(file);
        }
    }

    //递归展示目录
    public void showListR() {
        System.out.println("############## head ###################");
        for (File file : childFileList) {
            System.out.println("-" + file.getPath() + "\t\t" + file.getSize());
            if (file.isDir()) {
                ((FileDir)file).showListR();
            }
        }
        System.out.println("############### end ####################");

    }

}
